$(function () {
    $("#backPage").load("/html/backPage.html");
})
// 查询自己的所有日报
function findAll() {
    let data = {
        key: $("#selectKey").val(),
        startTime: $("#startTime").val(),
        endTime: $("#endTime").val(),
        pageNum: $("#pageNum").val(),
        pageSize: $("#pageSize").val()
    };
    console.log(data)
    let result = jsonGetValue("/d/daily/getSelectList", data);
    console.log(result);
    setAllData(result.data.list);
    $("#total").html(result.data.count);
}
function setAllData(data) {
    let html = "";
    for (let i=0; i<data.length;i++) {
        html += "<tr>\n" +
            '        <td><input type="checkbox" name="checkOne" value="' + data[i].id + '"></td>\n' +
            "        <td>"+ data[i].id +"</td>\n" +
            "        <td>"+ data[i].name +"</td>\n" +
            "        <td>"+ data[i].teacher +"</td>\n" +
            "        <td>"+ data[i].coach +"</td>\n" +
            "        <td>"+ data[i].content +"</td>\n" +
            "        <td>"+ data[i].gains +"</td>\n" +
            "        <td>"+ data[i].lack +"</td>\n" +
            "        <td>"+ data[i].dailyState +"</td>\n" +
            "        <td>"+ data[i].createTime +"</td>\n";
        html += "    <td>\n" +
            "            <div onclick='check(" + data[i].id +")' title='查看'>" +
            '            <a href="#check-modal-box" style="text-decoration:none;">' +
            '            <i class="iconfont icon-check">&#xe61c;</i></a></div>\n' +
            "            <div onclick='del(" + data[i].id +")' title='查看'>" +
            '            <a href="#del-modal-box" style="text-decoration:none;">' +
            '            <i class="iconfont icon-del">&#xe60b;</i></a></div>\n' +
            "        </td>\n" +
            "    </tr>";
    }

    $("#tbody").html(html)
    $("#tbody > tr > td").css({"vertical-align":"middle"})
    $("#tbody > tr > td:last-child").css({"display": "flex","justify-content": "flex-start"})
    $("#tbody > tr > td:last-child > div").css({"cursor": "pointer"})
    $("#tbody > tr > td:last-child > div").css({"margin-right":"10px"})
    myCheckAO();
}

// 根据id获取日报信息
function getDailyById(id) {
    let data = {id:id}
    let result = jsonGetValue("/d/daily/getDailyById", data);
    console.log(result);
    setDailyDataById(result.data.data);
}

// 给查看模态框赋值
function setDailyDataById(data) {
    $("#checkId").val(data.id);
    $("#checkName").val(data.name);
    $("#checkCreateTime").val(data.createTime);
    $("#checkTeacher").val(data.teacher);
    $("#checkCoach").val(data.coach);
    $("#checkContent").val(data.content);
    $("#checkGains").val(data.gains);
    $("#checkLack").val(data.lack);
    $("#delId").val(data.id);
    $("#tips").html("是否删除本条签到记录:" + data.name  +"?");
    if (data.stateCode == "draft"){
        let html = ' <a href="#close" title="关闭" class="modalBox-close-btn">\n' +
                   ' <input type="button" value="取消"></a>';
        $("#btnType").html(html);
    } else if (data.stateCode == "submitted"){
        let html = '<input type="button" value="已阅" id="checked" onclick="doCheck()">'+
                   ' <a href="#close" title="关闭" class="modalBox-close-btn">\n' +
                   ' <input type="button" value="取消"></a>';
        $("#btnType").html(html);
    } else {
        let html = '<input type="button" value="导出" id="created" onclick="createWord()">' +
                   ' <a href="#close" title="关闭" class="modalBox-close-btn">\n' +
                   ' <input type="button" value="取消"></a>';
        $("#btnType").html(html);
    }
}

// 弹出查看模态框
function check(id) {
    getDailyById(id);
}
// 执行批阅
function doCheck() {
    let data = {id: $("#checkId").val()};
    let result = jsonPost("/d/daily/checkDaily", data);
    console.log(result)
    if (result.data.data == 1) {
        $(".right").load("/html/daily/daily-list.html");
    }
}

// 获取查看模态框的值
function setCreateDaily() {
    let id =  $("#checkId").val();
    let name= $("#checkName").val();
    let createTime=  $("#checkCreateTime").val();
    let teacherName=  $("#checkTeacher").val();
    let coachName= $("#checkCoach").val();
    let content= $("#checkContent").val();
    let gains= $("#checkGains").val();
    let lack= $("#checkLack").val();
    let data = {
        id:id,
        name:name,
        createTime: createTime,
        teacher: teacherName,
        coach: coachName,
        content: content,
        gains: gains,
        lack: lack
    };
    return data;
}

// 导出日报
function createWord() {
    let data = setCreateDaily();
    let result = jsonPost("/d/daily/createWord", data);
    console.log(result);
    if (result.data == "ok"){
        alert("生成文档成功");
    }
}

// 弹出删除模态框
function del(id) {
    getDailyById(id);
}
// 执行删除
function doDel() {
    let data = {id: $("#delId").val()}
    let result = jsonPost("/d/daily/delDaily", data);
    console.log(result);
    if (result.data.result == 1) {
        $(".right").load("/html/daily/daily-list.html");
    }
}

// 获取多选框选择的日报
function getCheckDaily() {
    let daily = [];
    $("[name='checkOne']:checkbox").each(function(i){
        if ($(this).prop("checked") == true){
            daily.push(parseInt($(this).val()));
        }
    })
    daily = JSON.stringify(daily);
    daily = daily.replace("[","").replace("]","");
    return daily;
}
// 批量删除
function batchDeletion() {
    let daily = getCheckDaily();
    let data = {ids:daily}
    let result = jsonPostParam("/d/daily/batchDeletion", data);
    console.log(result);
    if (result.data.data = "success"){
        $(".right").load("/html/daily/daily-list.html");
    }
}
// 批量导出文档
function createDeletion() {
    let daily = getCheckDaily();
    let data = {ids:daily}
    let result = jsonPostParam("/d/daily/createDeletion", data);
    console.log(result);
    if (result.data == "ok"){
        $(".right").load("/html/daily/daily-list.html");
    }
}

// 查询点击事件
function select() {
    page("home");
}
// 清空点击事件
function clearAll() {
    $("input[ type='text']").val("");
    $("input[ type='date']").val("");
    $("#pageSize").val(10);
    $("#pageNum").val(1);
}